package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"oss-api/app/service"
	"oss-api/library/request"
	"oss-api/library/response"
)

var Install = new(InstallApi)

type InstallApi struct{}

// CheckConfig 检测配置文件是否存在
func (i *InstallApi) CheckConfig(c *gin.Context) {
	config, _ := service.Config.CheckConfig()
	c.JSON(http.StatusOK, response.Success(config))
}

// SettingDatabase 设置数据配置
func (i *InstallApi) SettingDatabase(c *gin.Context) {
	var param request.InstallDatabaseVo
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, response.Fail(err.Error()))
		return
	}
	flag, _ := service.Config.DatabaseConfig(param)
	c.JSON(http.StatusOK, response.Success(flag))
}

// SettingStorage 设置存储源配置
func (i *InstallApi) SettingStorage(c *gin.Context) {
	var param request.InstallStorageVo
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, response.Fail(err.Error()))
		return
	}
	flag, _ := service.Config.StorageConfig(param)
	c.JSON(http.StatusOK, response.Success(flag))
}

// SettingUser 设置系统账号密码
func (i *InstallApi) SettingUser(c *gin.Context) {
	var param request.InstallUsernameVo
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, response.Fail(err.Error()))
		return
	}
	flag, _ := service.Config.UserConfig(param)
	c.JSON(http.StatusOK, response.Success(flag))
}

// LoginUser 登陆用户
func (i *InstallApi) LoginUser(c *gin.Context) {
	var param request.InstallLoginVo
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, response.Fail(err.Error()))
		return
	}
	// 登陆失败
	res, err := service.Config.UserLogin(param)
	if err != nil {
		c.JSON(http.StatusBadRequest, response.Fail(err.Error()))
		return
	}
	// 登陆成功
	c.JSON(http.StatusOK, response.Success(res))

}
